﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class NewContract : CWSPage
{
    private int _cid = -1;
    protected int CID
    {
        get { return _cid; }
        set { _cid = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CID = String.IsNullOrEmpty(Request.QueryString["cid"])
                ? -1 : Int32.Parse(Request.QueryString["cid"].ToString());
            RegionBind();
            if (!String.IsNullOrEmpty(Request.QueryString["cid"]))
            {
                CIDlabel.Text = Request.QueryString["cid"].ToString();
                if (String.IsNullOrEmpty(Request.QueryString["renew"]))
                {
                    reset.Visible = false;
                    submit.Visible = false;
                    save.Visible = true;
                    delete.Visible = true;
                }
                else
                {
                    RenewLabel.Text = Request.QueryString["renew"].ToString();
                }
                ContractNo.Enabled = false;
                ContractBind();
            }
        }
    }

    protected void save_Click(object sender, EventArgs e)
    {
        string sqlstr = @"UPDATE CW_CONTRACT_T SET StationName = $StationName,StationType = $StationType,OtherName = $OtherName,BeginDate = $BeginDate
                                 ,EndDate = $EndDate,PayDate = $PayDate,ContractValue = $ContractValue,PayMode = $PayMode,ContactInfo = $ContactInfo,AccountName = $AccountName
                                 ,OpenBank = $OpenBank,AccountNo = $AccountNo,Desc = $Desc,Region = $Region      
                           WHERE CID = $CID";
        SQLiteParameter[] spc = {
            new SQLiteParameter("$StationName", DbType.String, 64),
            new SQLiteParameter("$StationType", DbType.String,6),
            new SQLiteParameter("$OtherName", DbType.String,32),
            new SQLiteParameter("$BeginDate", DbType.Date),
            new SQLiteParameter("$EndDate", DbType.Date),
            new SQLiteParameter("$PayDate", DbType.Date),
            new SQLiteParameter("$ContractValue", DbType.Int32),
            new SQLiteParameter("$PayMode", DbType.String, 4),
            new SQLiteParameter("$ContactInfo", DbType.String,32),
            new SQLiteParameter("$AccountName", DbType.String,10),
            new SQLiteParameter("$OpenBank", DbType.String,32),
            new SQLiteParameter("$AccountNo", DbType.String,32),
            new SQLiteParameter("$Desc", DbType.String,128),
            new SQLiteParameter("$Region", DbType.String,4),
            new SQLiteParameter("$CID", DbType.Int32)
        };
        spc[0].Value = StationName.Text.Trim();
        spc[1].Value = StationType.SelectedItem.Text;
        spc[2].Value = OtherName.Text.Trim();
        spc[3].Value = DateTime.Parse(BeginDate.Text);
        spc[4].Value = DateTime.Parse(EndDate.Text);
        spc[5].Value = DateTime.Parse(BeginDate.Text).AddYears(1);
        spc[6].Value = Int32.Parse(ContractValue.Text.Trim());
        spc[7].Value = PayMode.SelectedItem.Text;
        spc[8].Value = ContactInfo.Text.Trim();
        spc[9].Value = AccountName.Text.Trim();
        spc[10].Value = OpenBank.Text.Trim();
        spc[11].Value = AccountNo.Text.Trim();
        spc[12].Value = Desc.Value.Trim();
        spc[13].Value = RegionList.SelectedValue;
        spc[14].Value = Int32.Parse(CIDlabel.Text);
        int res = SQLiteHelper.ExecuteNonQuery(sqlstr, spc);
        if (res == 1)
        {
            Response.Redirect("ContractList.aspx");
        }
        else
        {
            msg.Text = "更新未成功！请重试";
        }
    }

    protected void submit_Click(object sender, EventArgs e)
    {
        if (AddContract())
        {
            Response.Redirect("ContractList.aspx");
        }
        else
        {
            msg.Text = "添加未成功！请重试";
        }
    }

    protected void delete_Click(object sender, EventArgs e)
    {
        string sqlstr = "UPDATE CW_CONTRACT_T SET Status = '删除' WHERE CID = $CID";
        SQLiteParameter[] spc = {
            new SQLiteParameter("$CID", DbType.Int32)
        };
        spc[0].Value = Int32.Parse(CIDlabel.Text);
        int res = SQLiteHelper.ExecuteNonQuery(sqlstr, spc);
        if (res == 1)
        {
            Response.Redirect("ContractList.aspx");
        }
        else
        {
            msg.Text = "删除未成功！请重试";
        }
    }

    private void RegionBind()
    {
        string sqlstr = "SELECT DataCode,DataName FROM CW_PUBDATA_T WHERE DataType = 'Region'";
        DataSet ds = SQLiteHelper.ExecuteDataset(sqlstr, null);
        RegionList.DataSource = ds;
        RegionList.DataTextField = "DataName";
        RegionList.DataValueField = "DataCode";
        RegionList.DataBind();
        RegionList.SelectedIndex = -1;
    }

    private void ContractBind()
    {
        string sqlstr = @"SELECT Region,StationName,StationType,OtherName,ContractNo,BeginDate,EndDate,ContractValue,PayMode,ContactInfo,AccountName,OpenBank,AccountNo
            ,Desc FROM cw_Contract_t WHERE CID = $CID";
        SQLiteParameter[] spc = {
            new SQLiteParameter("$CID", DbType.Int32)
        };
        spc[0].Value = CID;
        DataRow cont = SQLiteHelper.ExecuteDataRow(sqlstr,spc);
        CIDlabel.Text = CID.ToString();
        RegionList.SelectedValue = cont["Region"].ToString();
        StationName.Text = cont["StationName"].ToString();
        StationType.SelectedValue = cont["StationType"].ToString();
        OtherName.Text = cont["OtherName"].ToString();
        ContractNo.Text = cont["ContractNo"].ToString();
        BeginDate.Text = DateTime.Parse(cont["BeginDate"].ToString()).ToString("yyyy-MM-dd");
        EndDate.Text = DateTime.Parse(cont["EndDate"].ToString()).ToString("yyyy-MM-dd");
        ContractValue.Text = cont["ContractValue"].ToString();
        PayMode.SelectedValue = cont["PayMode"].ToString();
        ContactInfo.Text = cont["ContactInfo"].ToString();
        AccountName.Text = cont["AccountName"].ToString();
        OpenBank.Text = cont["OpenBank"].ToString();
        AccountNo.Text = cont["AccountNo"].ToString();
        Desc.Value = cont["Desc"].ToString();
    }

    private bool AddContract()
    {
        Renew();
        string sqlstr = @"insert into cw_contract_t(cid,StationName,StationType,OtherName,ContractNo,BeginDate,EndDate,PayDate,ContractValue,PayMode,ContactInfo,AccountName,OpenBank
                                                    ,AccountNo,Desc,Status,Operator,OpeDate,Region) 
                        values(null,$StationName,$StationType,$OtherName,$ContractNo,$BeginDate,$EndDate,$PayDate,$ContractValue,$PayMode,$ContactInfo,$AccountName,$OpenBank,$AccountNo
                                ,$Desc,$Status,$Operator,$OpeDate,$Region)";
        SQLiteParameter[] spc = {
            new SQLiteParameter("$StationName", DbType.String, 64),
            new SQLiteParameter("$StationType", DbType.String,6),
            new SQLiteParameter("$OtherName", DbType.String,32),
            new SQLiteParameter("$ContractNo", DbType.String,64),
            new SQLiteParameter("$BeginDate", DbType.Date),
            new SQLiteParameter("$EndDate", DbType.Date),
            new SQLiteParameter("$PayDate", DbType.Date),
            new SQLiteParameter("$ContractValue", DbType.Int32),
            new SQLiteParameter("$PayMode", DbType.String, 4),
            new SQLiteParameter("$ContactInfo", DbType.String,32),
            new SQLiteParameter("$AccountName", DbType.String,10),
            new SQLiteParameter("$OpenBank", DbType.String,32),
            new SQLiteParameter("$AccountNo", DbType.String,32),
            new SQLiteParameter("$Desc", DbType.String,128),
            new SQLiteParameter("$Status", DbType.String,4),
            new SQLiteParameter("$Operator", DbType.Int32),
            new SQLiteParameter("$OpeDate", DbType.Date),
            new SQLiteParameter("$Region", DbType.String,4)
        };
        spc[0].Value = StationName.Text.Trim();
        spc[1].Value = StationType.SelectedItem.Text;
        spc[2].Value = OtherName.Text.Trim();
        spc[3].Value = ContractNo.Text.Trim();
        spc[4].Value = DateTime.Parse(BeginDate.Text);
        spc[5].Value = DateTime.Parse(EndDate.Text);
        spc[6].Value = DateTime.Parse(BeginDate.Text).AddYears(1);
        spc[7].Value = Int32.Parse(ContractValue.Text.Trim());
        spc[8].Value = PayMode.SelectedItem.Text;
        spc[9].Value = ContactInfo.Text.Trim();
        spc[10].Value = AccountName.Text.Trim();
        spc[11].Value = OpenBank.Text.Trim();
        spc[12].Value = AccountNo.Text.Trim();
        spc[13].Value = Desc.Value.Trim();
        spc[14].Value = "正常";
        spc[15].Value = Uid;
        spc[16].Value = System.DateTime.Now;
        spc[17].Value = RegionList.SelectedValue;
        int result = SQLiteHelper.ExecuteNonQuery(sqlstr, spc);
        return result == 1;
    }

    private void Renew()
    {
        if (RenewLabel.Text.Length > 0)
        {
            string sqlstr = "UPDATE CW_CONTRACT_T SET Status = '续签' WHERE CID = $CID";
            SQLiteParameter[] spc = {
                new SQLiteParameter("$CID", DbType.Int32)
            };
            spc[0].Value = Int32.Parse(CIDlabel.Text);
            SQLiteHelper.ExecuteNonQuery(sqlstr, spc);
        }
    }
}
